﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataTier;
using System.Data.SqlClient;
using System.Data.Common;
using System.Threading;

namespace DbLayer
{
    public class DbReservation : DbObject, IDbReservation
    {

        private static DbReservation _instance = null;
        private IDbPartReservation _dbPartReservation = DbPartReservation.Instance;
        private IDbPerson _dbPerson = DbPerson.Instance;

        private DbReservation()
        {
        }

        public static IDbReservation Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DbReservation();
                }
                return _instance;
            }
        }

        public void AddReservation(Reservation reservation)
        {

            int reservationNr = GetNextId("Reservation", "reservationNr");

            string statement = BuildInsertStatement("Reservation", reservationNr.ToString(), reservation.Customer.CustomerId.ToString());
            ExecuteStatement(statement);

            foreach (PartReservation partReservation in reservation.PartReservations)
            {
                string partReservationStatement = BuildInsertStatement("PartReservation", reservationNr.ToString(), partReservation.PickUpDate.ToString("yyyy-MM-dd HH:mm:ss"), partReservation.ExpirationDate.ToString("yyyy-MM-dd HH:mm:ss"), partReservation.Station.Id.ToString());
                ExecuteStatement(partReservationStatement);
            }

        }

        public Reservation GetReservation(int reservationNr)
        {

            Reservation reservation = null;
            string statement = BuildSelectStatement("Reservation", "reservationNr = " + reservationNr.ToString());
            SqlDataReader reader = ExecuteQuery(statement);

            if (reader.Read())
            {
                reservation = BuildReservation(reader);
            }

            return reservation;

        }

        public IList<Reservation> GetReservations(int customerId)
        {

            List<Reservation> reservations = new List<Reservation>();

            string query = BuildSelectStatement("Reservation", "customerId = '" + customerId.ToString() + "'");
            SqlDataReader reader = ExecuteQuery(query);

            while (reader.Read())
            {
                reservations.Add(BuildReservation(reader));
            }

            reader.Close();

            return reservations;

        }

        public IList<Reservation> GetAllReservations()
        {

            List<Reservation> reservations = new List<Reservation>();

            string query = BuildSelectStatement("Reservation", "");
            SqlDataReader reader = ExecuteQuery(query);

            while (reader.Read())
            {
                reservations.Add(BuildReservation(reader));
            }

            reader.Close();

            return reservations;

        }

        public void RemoveReservation(int reservationNr)
        {

            _dbPartReservation.RemovePartReservations(reservationNr);

            string statement = BuildDeleteStatement("Reservation", "reservationNr = " + reservationNr.ToString());
            ExecuteStatement(statement);

        }

        private Reservation BuildReservation(SqlDataReader reader)
        {

            Reservation reservation = new Reservation();

            reservation.ReservationNr = Convert.ToInt32(reader["reservationNr"]);
            reservation.PartReservations = _dbPartReservation.GetPartReservations(reservation.ReservationNr);
            reservation.Customer = _dbPerson.GetCustomer(Convert.ToInt32(reader["customerId"]));

            return reservation;

        }
    }
}
